function doChooseFriends(propertyKey, propertyValue) {
	var hscroll = (document.all ? document.scrollLeft : window.pageXOffset);
	var vscroll = (document.all ? document.scrollTop : window.pageYOffset);
	$('.sharebet_component').css('top', vscroll + 10);
	chooseFriends(propertyKey, propertyValue);
}

function chooseFriends(propertyKey, propertyValue) {
	var html = ""
		+ '<form action="" id="frmShare" rel="async" method="post" novalidate="novalidate">'
		+ '<input type="hidden" id="frmShare_share_propertyKey" value="' + propertyKey + '"/>'
		+ '<input type="hidden" id="frmShare_share_propertyValue" value="' + propertyValue + '"/>'
		+ '<table width="100%" border="0" cellpadding="0" cellspacing="0" id="tblFriendList" class="closeuicontainer">'
	    + '<thead>'
	    + '  <tr>'
	    + '    <td colspan="4" class="bottomborder"><strong>' + label_share_title + '</strong><div id="_btnClose" style="display: none;" class="closeuiicon" onclick="$(\'.sharebet_component\').html(\'\');" ></div></td>'
	    + '  </tr>'
	    + '  <tr>'
	    + '    <td colspan="4" class="alignright"><a href="javascript: selectAll(\'chkFriendIdList\');">Select All</a>&nbsp;&nbsp;&nbsp;<a href="javascript: unSelectAll(\'chkFriendIdList\');">UnSelect All</a></td>'
	    + '  </tr>'
	    + '</thead>'
	    + '  <tr>'
	    + '    <td width="100%"><div class="scrollbox sharefriendlist_box"><table width="100%" border="0" cellpadding="2" cellspacing="0">'
	    + '    <tbody id="tbFriendList"></tbody>'
		+ '    <tfoot id="tbFriendListMore"></tfoot>'
		+ '  </table></div></td>'
		+ '</tr>'
		+ '<tfoot>'
		+ '  <tr>'
		+ '    <td width="30" colspan="4" class="topbordergray"><a href="javascript: doShareBet();">' + label_share_share + '</a></td>'
		+ '  </tr>'
		+ '</tfoot>'
		+ '</table></form>';
	$('.sharebet_component').hide();
	$('.sharebet_component').html(html);
	loadFriends(propertyKey, propertyValue);
}

function loadFriends(propertyKey, propertyValue, append, indexPage) {
	$.ajax({
		url : LISTFRIEND_AJAX_URL,
		dataType : 'json',
		type: 'post',
		iframe : true,
		data : {
			'notificationInfo.propertyKey': propertyKey,
			'notificationInfo.propertyValue': propertyValue,
			'pagingInfo.indexPage': indexPage
		},
		success : function(data) {
			if(data.errorFlag == 0) {
				if(data.friendList == null || data.friendList.length <= 0) {
					$('.sharebet_component').hide();
					return;
				}
				loadFriendsComplete(propertyKey, propertyValue, data, append);
			} else {
				if(data.errorFlag == 1) {
					window.open(data.redirectURL);
				} else {
					$('.sharebet_component').html("");
				}
			}
		},
		error : function(xhr, ajaxOptions, thrownError) {
			//alert(xhr.status);
			//alert(thrownError);
		}
	});
}

function loadFriendsComplete(propertyKey, propertyValue, data, append) {
	if(data.friendList == null || data.friendList.length <= 0) {
		$('.sharebet_component').html("");
		return;
	}
	var friendList = data.friendList;
	var shared = data.shared;
	var html = '';
	for(var i = 0; i < friendList.length; i++) {
		var friend = friendList[i];
		var imagePath = getImage(friend.displayImage);
		var sharedCount = '';
		if(shared != null && shared[friend.customerId] != null && shared[friend.customerId] > 0) {
			sharedCount = ' (' + shared[friend.customerId] + ' shared)';
		}
		html = html + ''
			+ '    <tr>'
			+ '      <td width="100%"><table cellspacing="0" cellpadding="0" border="0" width="100%">'
			+ '        <tr>'
			+ '          <td width="30" rowspan="2"><img src="' + imagePath + '" alt="' + NO_PICTURE_ALT + '" width="30" height="30" /></td>'
			+ '          <td width="5" rowspan="2">&nbsp;</td>'
			+ '          <td align="left" class="valigntop"><a href="' + BET_LIST_RECENTBET_URL + '?betInfo.customerId=' + friend.customerId + '">' + friend.displayName + '</a></td>'
			+ '          <td width="30" class="alignright" valign="top"><input type="checkbox" name="friendIdList" id="chkFriendIdList" value="' + friend.customerId + '" /><input type="hidden" id="__checkbox_chkFriendIdList" name="__checkbox_friendIdList" value="' + friend.customerId + '" /></td>'
			+ '        </tr>'
			+ '        <tr>'
			+ '          <td class="alignleft">' + sharedCount + '</td>'
			+ '          <td>&nbsp;</td>'
			+ '        </tr>'
			+ '      </table></td>'
			+ '    </tr>';
	}
	var pagingInfo = data.pagingInfo;
	if(pagingInfo && pagingInfo.hasNextPage == true) {
		$('#tbFriendListMore').html('' 
				+ '<tr>'
				+ '  <td class="aligncenter"><a href="javascript: loadFriends(\'' + propertyKey + '\',\'' + propertyValue + '\', true, ' + (pagingInfo.indexPage + 1) +');">' + pagingInfo.remainingItems + ' MORE FRIENDS</a></td>'
				+ '</tr>');
	} else {
		$('#tbFriendListMore').html('');
	}
	if(append) {
		$('#tbFriendList').append(html);
	} else {
		$('#tbFriendList').html(html);
	}
	$('.sharebet_component').show();
	applyCloseUIButton();
	validateShareBet();
}

function validateShareBet() {
	$.validator.addMethod("atLeastCheck", function(value, element) {
		var list = $('#frmShare [name="friendIdList"]');
		var isChecked = false;
		if (list != null) {
			for ( var i = 0; i < list.length; i++) {
				if (list[i].checked == true) {
					isChecked = true;
				}
			}
		}
		return isChecked;
	});
	$("#frmShare").validate({
		rules: {
		},
		messages: {
		},
		submitHandler : function(form) {
			shareBet();
		}
	});
	$('#frmShare [name="friendIdList"]').each(function() {
		$(this).rules("add", {
			atLeastCheck : true,
			messages : {
				atLeastCheck : message_bet_sharebet_required
			}
		});
	});
}

function doShareBet() {
	$('#frmShare').submit();	
}

function shareBet() {
	var propertyKey = $('#frmShare_share_propertyKey').val();
	var propertyValue = $('#frmShare_share_propertyValue').val();
	$('#frmShare').ajaxSubmit({
		url : SHAREBET_AJAX_URL,
		type: 'post',
		dataType : 'json',
		data: {
			'notificationInfo.propertyKey': propertyKey,
			'notificationInfo.propertyValue': propertyValue
		},
		iframe : true,
		success : function(data) {
			if(data.errorFlag == 0) {
				$('.sharebet_component').html('');
			}
		},
		error : function(xhr, ajaxOptions, thrownError) {
			//alert(xhr.status);
			//alert(thrownError);
		}
	});
}